package day27;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

//通过Socket创建服务端和客户端  通过客户端
// 和服务端 实现传输User对象(自定义)
// 提示:服务端做序列化(传输对象给客户端)
// 客户端做反序列化(读取服务端写入的对象)
public class WorkServer1 {
    public static void main(String[] args) throws IOException {
        ServerSocket server=new ServerSocket(9999);
        User user=new User("张三","男",new Date());
        Socket client= server.accept();
        System.out.println(client.getInetAddress()+"连接成功！");
        ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
        oos.writeObject(user);
    }
}

class WorkCilent1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Socket client=new Socket("127.0.0.1",9999);
        ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
        User user=(User)ois.readObject();
        System.out.println("接收的数据为:"+user);
    }
}

class User implements Serializable{
    String name;
    String sex;
    Date birthday;

    public User(String name, String sex, Date birthday) {
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
    }
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}